package com.avalon.core.select;

import com.avalon.core.condition.Condition;
import com.avalon.core.select.builder.SelectBuilder;
import com.avalon.core.service.AbstractService;

import java.util.ArrayList;

public class ConditionFieldList extends ArrayList<ConditionField> {

    public ConditionField addCondition(Condition condition, AbstractService service) {
        ConditionField conditionField = new ConditionField(condition.getName(), condition, service);
        add(conditionField);
        return conditionField;
    }

    public ConditionField addCondition(ConditionField conditionField) {
        add(conditionField);
        return conditionField;
    }

    public StringBuilder getConditionSql(SelectBuilder selectBuilder) {
        StringBuilder conditionSql = new StringBuilder();
        for (ConditionField conditionField : this) {
            StringBuilder x = conditionField.getConditionSql(selectBuilder);
            if (!x.isEmpty()) {
                if (conditionSql.length() > 0) {
                    conditionSql.append(" and ");
                }
                conditionSql.append("(").append(x).append(")");
            }
        }
        return conditionSql;
    }
}
